#!/usr/bin/env slua

package.path  = "/usr/milis/mps/lua/?.lua" .. ";".. package.path
package.path  = "/etc/init/?.lua"     .. ";".. package.path

local c = require ("config")
local serpent = require('serpent')

local config_file="/etc/init/config.lua"

local key=arg[1]
local val=arg[2]

function tprint(tablo)
	local data=serpent.block(tablo)
	if val ~=nil then
		-- açık çıktıyı kullan
		io.output(io.open(config_file, "w"))
		io.write("local config=")
		io.write(data)
		io.write("\n")
		io.write("return config")
		io.write("\n")  
		io.close()
	else
		print(data)
    end
end

function cprint(value)
	print(value)
	os.exit()
end

if key~= nil then
	local poz=key:find(".",1,true)
	if poz ~= nil then
		key1=key:sub(1,poz-1)
		key2=key:sub(poz+1,-1)
		if key2 == "1" then key2=1 end
		if key2 == "2" then key2=2 end
		if key2 == "3" then key2=3 end
		if val ~=nil then
			if val == "nil" then c[key1][key2]=nil
			elseif val == "" then c[key1][key2]=""
			else c[key1][key2]=val end
		else
			cprint(c[key1][key2])
		end		
	elseif key == "action" then
		if val == "execute" then
			c.action=os.execute
		else
			c.action=print
		end
	else
		if val ~=nil then
			c[key]=val
		else
			cprint(c[key])
		end
	end
end

tprint(c)


